<?php defined('ALLOWED') OR die('No direct access allowed.');

class Loader
{
    private static $_controller,
                   $_model,
                   $_lib,
                   $_helper,
                   $_api,
                   $_exception;
            
    private static $_rules = array();
    
    private static $_allowedClasses = array( 'Controller', 'Model', 'Api', 'Exception', 'Helper');
    
    public function __construct()
    {
        self::$_rules = array(
            'Controller' => array(
                'path' => 'controllers' . FILE_SEPARATOR,
                'system' => 1,
                'info' => '_controller',
            ),
            
            'Model' => array(
                'path' => 'models' . FILE_SEPARATOR,
                'system' => 0,
                'info' => '_model',
            ),
            
            'Api' => array(
                'path' => 'vendors' . FILE_SEPARATOR . 'api' . FILE_SEPARATOR,
                'system' => 1,
                'info' => '_api',
            ),
            
            'Exception' => array(
                'path' => 'vendors' . FILE_SEPARATOR . 'exception' . FILE_SEPARATOR,
                'system' => 1,
                'info' => '_exception',
            ),
            
            'Helper' => array(
                'path' => 'vendors' . FILE_SEPARATOR . 'helper' . FILE_SEPARATOR,
                'system' => 1,
                'info' => '_helper',
            )
        );
    }
            
    public static function auto()
    {        
        spl_autoload_register('self::myLoader');
    }
    
    /**
     * myLoader method
     * load allowed class 
     */
    private static function myLoader( $className)
    {
        //regexp return me file type
        preg_match_all( '^([A-Z]{1}.[a-z\_]+)^', $className, $match);
        $match = array_reverse( $match[0]);
        
        if( in_array( $match[0], self::$_allowedClasses))
        {
            if( file_exists( 'app'. FILE_SEPARATOR . self::$_rules[$match[0]]['path'] . $className . '.php'))
            {
                //require
                require 'app'. FILE_SEPARATOR . self::$_rules[$match[0]]['path'] . $className . '.php';
                
                //set info on loading elements
                self::${self::$_rules[$match[0]]['info']}[] = array(
                'fileName' => $className,
                'filePath' => 'app'. FILE_SEPARATOR . self::$_rules[$match[0]]['path'] . $className . '.php',
                );
            }
            
            else
            {   
                if( self::$_rules[$match[0]]['system'])
                {
                    //require from system
                    require 'smilodon'. FILE_SEPARATOR . self::$_rules[$match[0]]['path'] . $className . '.php';
                    
                    //set info on loading elements
                    self::${self::$_rules[$match[0]]['info']}[] = array(
                    'fileName' => $className,
                    'filePath' => 'smilodon'. FILE_SEPARATOR . self::$_rules[$match[0]]['path'] . $className . '.php',
                    );
                }
            }
        }
        
        #others class, or libs
        else
        {
            if( file_exists( 'smilodon'. FILE_SEPARATOR . 'vendors' . FILE_SEPARATOR . 'lib' . FILE_SEPARATOR . $className . '.php'))
            {
                //require
                require 'app'. FILE_SEPARATOR . 'vendors' . FILE_SEPARATOR . 'lib' . FILE_SEPARATOR . $className . '.php';
                
                //set info on loading elements
                $this -> _lib[] = array(
                'fileName' => $className,
                'filePath' => 'smilodon'. FILE_SEPARATOR . 'vendors' . FILE_SEPARATOR . 'lib' . FILE_SEPARATOR . $className . '.php',
                );
            }
            
            else
            {
                //require from system
                require 'app'. FILE_SEPARATOR . 'vendors' . FILE_SEPARATOR . 'lib' . FILE_SEPARATOR . $className . '.php';
                
                //set info on loading elements
                $this -> _lib[] = array(
                'fileName' => $className,
                'filePath' => 'app'. FILE_SEPARATOR . 'vendors' . FILE_SEPARATOR . 'lib' . FILE_SEPARATOR . $className . '.php',
                );
            }
        }
    }
}
